/**
 * @author: Miss.杨
 * @date: 2025年3月16日 15:32:05
 * @description: 
 */
package com.sheepone.monitor.audit.autoconfigure;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import com.sheepone.monitor.audit.AuditMonitorBootClient;
import com.sheepone.monitor.audit.aspect.AuditCommitAspect;
import com.sheepone.monitor.audit.listener.SceneProcessorApplicationListener;

/**
 * @author: Miss.杨
 * @date: 2025年3月16日 15:32:05
 * @description:
 */
@AutoConfiguration
@EnableConfigurationProperties(AuditMonitorClientBootConfig.class)
public class AuditMonitorClientBootAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public AuditMonitorBootClient monitorClient(AuditMonitorClientBootConfig config) {
        AuditMonitorBootClient client = new AuditMonitorBootClient(config);
        client.run();
        return client;
    }

    /**
     * ! 这里使用@ConditionalOnExpression是因为@ConditionalOnProperty大小写敏感
     * 
     * @param config
     * @return
     */
    @Bean
    @ConditionalOnExpression("${sheepone.monitor.client.audit.autoCommit:false} || ${sheepone.monitor.client.audit.auto-commit:false}")
    public AuditCommitAspect auditContextAspect(AuditMonitorClientBootConfig config) {
        return new AuditCommitAspect(config.getAudit());
    }

    /**
     * 
     * @param config
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnExpression("${sheepone.monitor.client.audit.sceneEnable:false} || ${sheepone.monitor.client.audit.scene-enable:false}")
    public SceneProcessorApplicationListener sceneProcessorApplicationListener() {
        return new SceneProcessorApplicationListener();
    }
}
